package DMSXL._2024.BinaryTrees;

import java.util.*;

public class levelOrder {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrder(TreeNode root){
        List<List<Integer>> result = new ArrayList<>();
        Queue<TreeNode> stack = new LinkedList<>();
        if(root == null){
            return result;
        }
        stack.offer(root);
        while (!stack.isEmpty()){
            List<Integer> temp = new ArrayList<>();
            int n = stack.size();
            for (int i = 1; i <= n; i++) {
                TreeNode cur = stack.poll();
                temp.add(cur.val);
                if(cur.left != null){
                    stack.offer(cur.left);
                }
                if(cur.right != null){
                    stack.offer(cur.right);
                }
            }
            result.add(temp);
        }
        return result;
    }
}
